<?php

namespace App\Admin\Controllers;

use App\Exports\TeacherEnroll;
use App\Http\Traits\GlobalConstant;
use App\Models\CourseDuty;
use App\Models\CourseOrder;
use App\Models\Estimate;
use App\Models\Teacher;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class TeacherStaticsController extends BaseController
{

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('学员库列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('学员库详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑学员库')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增学员库')
            ->description('新增学员库')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Teacher());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('id', '教师ID');
            $filter->like('name', '教师姓名');
            $filter->like('mobile', '教师电话');
        });
//
        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', '教师ID')->sortable();
        $grid->column('name', '姓名');
        $grid->column('mobile', '电话');
        $grid->column('userInfo.score', '积分');
        $grid->column('aaa', '已报学员')->display(function () {
            return CourseOrder::whereIn('course_id', function ($q){
                $q->where('teacher_id',$this->id)->select('id')->from('course');
            })->where('status', '!=', -1)->where('status', '!=', 1)->where('status', '!=', 4)->distinct('user_id')->count();
        });
        $grid->column('bbb','满意度')->display(function ($v){
            $avg = Estimate::where('teacher_id', $this->id)->avg('teacher_score');
            if (empty($avg)) {
                return '-';
            }
            return sprintf("%.2f", $avg);
        });
        $grid->disableActions();
        $grid->exporter(new TeacherEnroll());
        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();

            });
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Teacher::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Teacher());
        $form->text('name', '姓名')->required()->attribute(['autocomplete' => 'off']);
        $form->mobile('mobile', '手机')->required();
//        $form->select('user_id','关联用户')->required()->options(Users::where('status',1)->pluck('nickname as text','id'))
//            ->creationRules(['required', "unique:teacher"],['unique'=>'当前选择的用户已被关联'])
//            ->updateRules(['required', "unique:teacher,user_id,{{id}}"],['unique'=>'当前选择的用户已被关联']);
        $form->image('avatar', '头像')->required()->uniqueName()->removable();
        $form->number('age', '年龄')->required()->min(1)->default(1);
//        $form->text('edu_max', '最高学历');
        $form->radio('sex', '性别')->default(1)->options(GlobalConstant::SEX_LIST);
        $form->radio('status', '状态')->options(GlobalConstant::_STATUS)->default(1);
        $form->textarea('intro', '简介');
        $form->select('approve_status', '审核状态')->options([1 => '未审核', 2 => '审核通过', 3 => '审核失败'])
            ->when(3, function (Form $form) {
                $form->textarea('approve_remark', '未通过原因');
            });
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }

    public function update1($id)
    {
        $param                = request();
        $info                 = Teacher::find($id);
        $info->approve_status = $param['approve_status'];
        if ($param['approve_status'] == 3 && empty($param['approve_remark'])) {
            admin_error('请填写不通过原因');
            return back();
        }
        $info->approve_remark = $param['approve_remark'];
        $info->status         = $param['status'];
        $info->save();
        if ($param['approve_status'] == 2) {
            $user             = Users::where('id', $info->user_id)->first();
            $user->teacher_id = $info->id;
            $user->save();
        }
        admin_success('保存成功');
        return back();
    }
}
